﻿''
''Copyright Zac McKenzie 2012
''
Imports sxl = System.Xml.Linq
Imports System.Linq
Public Class svc
    Public channelId As Integer
    Public channelName As String
    Public onId As String
    Public tsId As String

    Public Sub New(ByVal _channelId As Integer, ByVal _channelName As String, ByVal _onId As String, ByVal _tsId As String)
        channelId = _channelId
        channelName = _channelName
        onId = _onId
        tsId = _tsId
    End Sub

    Sub New()

    End Sub

    Public Function toParamArray() As String()
        Dim pa(3) As String
        pa(0) = channelId.ToString
        pa(1) = channelName
        pa(2) = onId
        pa(3) = tsId
        Return pa
    End Function

End Class

Public Class config



    Public Shared channelList As New Dictionary(Of Integer, svc)

    Public Shared noOfTimers As Integer
    Dim timerList As New List(Of scheduledEvent)
    Public Shared ipAddress As String
    Public Shared refreshDays As Integer = 99
    Public Shared noOfDaysToDownload As Integer = 7
    Public Shared noTimers As Integer = 1
    Public Shared tvListingDirectory = "c:\tv"
    Public Shared ozTivoUserName
    Public Shared ozTivoUserPassword

    Public Shared fromEmail As String
    Public Shared toEmail As String
    Public Shared emailSubject As String
    Public Shared emailUserId As String
    Public Shared emailPassword As String
    Public Shared serverHost As String
    Public Shared serverPort As String

    Public Shared ratingThreashold As String = 7

    Public Shared progWatchList As List(Of String)
    Public Shared catWatchList As List(Of String)


    Public Shared Sub setAll(ByVal _ipAddress As String,
                             ByVal _refreshDays As Integer,
                             ByVal _noOfDaysToDownload As Integer,
                             ByVal _noTimers As Integer,
                             ByVal _tvListingDirectory As String,
                             ByVal _ozTivoUserName As String,
                             ByVal _ozTivoUserPassword As String,
                             ByVal _fromEmail As String,
                             ByVal _toEmail As String,
                             ByVal _emailSubject As String,
                             ByVal _emailUserId As String,
                             ByVal _emailPassword As String,
                             ByVal _serverHost As String,
                             ByVal _serverPort As String,
                             ByVal _ratingThreashold As String)

        ipAddress = _ipAddress
        refreshDays = _refreshDays
        noOfDaysToDownload = _noOfDaysToDownload
        noTimers = _noTimers
        tvListingDirectory = _tvListingDirectory
        ozTivoUserName = _ozTivoUserName
        ozTivoUserPassword = _ozTivoUserPassword

        fromEmail = _fromEmail
        toEmail = _toEmail
        emailSubject = _emailSubject
        emailUserId = _emailUserId
        emailPassword = _emailPassword
        serverHost = _serverHost
        serverPort = _serverPort

        ratingThreashold = _ratingThreashold

    End Sub

    Public Shared Sub getAll(ByRef _ipAddress As String,
                             ByRef _refreshDays As Integer,
                             ByRef _noOfDaysToDownload As Integer,
                             ByRef _noTimers As Integer,
                             ByRef _tvListingDirectory As String,
                             ByRef _ozTivoUserName As String,
                             ByRef _ozTivoUserPassword As String,
                             ByRef _fromEmail As String,
                             ByRef _toEmail As String,
                             ByRef _emailSubject As String,
                             ByRef _emailUserId As String,
                             ByRef _emailPassword As String,
                             ByRef _serverHost As String,
                             ByRef _serverPort As String,
                             ByRef _ratingThreashold As String)
        _ipAddress = ipAddress
        _refreshDays = refreshDays
        _noOfDaysToDownload = noOfDaysToDownload
        _noTimers = noTimers
        _tvListingDirectory = tvListingDirectory
        _ozTivoUserName = ozTivoUserName
        _ozTivoUserPassword = ozTivoUserPassword

        _fromEmail = fromEmail
        _toEmail = toEmail
        _emailUserId = emailUserId
        _emailPassword = emailPassword
        _serverHost = serverHost
        _serverPort = serverPort

        _ratingThreashold = ratingThreashold

    End Sub

    Public Shared Sub loadConfig()
        Dim doc As sxl.XDocument

        If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & "\serviceNames.xml") Then
            doc = sxl.XDocument.Load(My.Application.Info.DirectoryPath & "\serviceNames.xml")

            Dim tvDir = doc.Descendants("tvDir").Value
            config.ipAddress = doc.Descendants("ipAddress").Value
            config.refreshDays = doc.Descendants("refreshDays").Value
            config.noOfDaysToDownload = doc.Descendants("noOfDaysToDownload").Value
            config.noTimers = doc.Descendants("noTimers").Value
            config.ozTivoUserName = doc.Descendants("ozTivoUserName").Value
            config.ozTivoUserPassword = doc.Descendants("ozTivoPassword").Value

            config.fromEmail = doc.Descendants("fromEmail").Value
            config.toEmail = doc.Descendants("toEmail").Value
            config.emailSubject = doc.Descendants("emailSubject").Value
            config.emailUserId = doc.Descendants("emailUserId").Value
            config.emailPassword = doc.Descendants("emailPassword").Value
            config.serverHost = doc.Descendants("serverHost").Value
            config.serverPort = doc.Descendants("serverPort").Value

            config.ratingThreashold = doc.Descendants("ratingThreashold").Value

            For Each t As sxl.XElement In doc.Descendants("service")
                Try
                    config.channelList.Add(t.@serviceNum, New svc(t.@serviceNum, t.@channelName, t.@onId, t.@tsId))
                Catch ex As Exception
                    MessageBox.Show("Can't add " & t.@serviceNum & "/" & t.@channelName & " to service channel list, probably because it is a duplicate")
                End Try
            Next

        End If

        If Not My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & "\watchNames.xml") Then Exit Sub
        Dim doc2 As sxl.XDocument = sxl.XDocument.Load(My.Application.Info.DirectoryPath & "\watchNames.xml")

        Dim sl As New sxl.XElement("watchList")
        progWatchList = New List(Of String)
        For Each e As sxl.XElement In doc2.Descendants("program")
            progWatchList.Add(e.@name)
        Next

        catWatchList = New List(Of String)
        For Each e As sxl.XElement In doc2.Descendants("Category")
            catWatchList.Add(e.@name)
        Next
    End Sub
    Public Shared Sub saveConfig()
        Dim sl As New sxl.XElement("serviceList")
        Dim doc As New sxl.XDocument
        doc.Add(sl)

        Dim tvDir As New sxl.XElement("tvDir")
        tvDir.Value = config.tvListingDirectory
        sl.Add(tvDir)

        Dim ipAddress As New sxl.XElement("ipAddress")
        ipAddress.Value = config.ipAddress
        sl.Add(ipAddress)

        Dim refreshDays As New sxl.XElement("refreshDays")
        refreshDays.Value = config.refreshDays
        sl.Add(refreshDays)

        Dim noOfDaysToDownload As New sxl.XElement("noOfDaysToDownload")
        noOfDaysToDownload.Value = config.noOfDaysToDownload
        sl.Add(noOfDaysToDownload)

        Dim noTimers As New sxl.XElement("noTimers")
        noTimers.Value = config.noTimers
        sl.Add(noTimers)

        Dim ozTivoUserName As New sxl.XElement("ozTivoUserName")
        ozTivoUserName.Value = config.ozTivoUserName
        sl.Add(ozTivoUserName)

        Dim ozTivoPassword As New sxl.XElement("ozTivoPassword")
        ozTivoPassword.Value = config.ozTivoUserPassword
        sl.Add(ozTivoPassword)

        Dim fromEmail As New sxl.XElement("fromEmail")
        fromEmail.Value = config.fromEmail
        sl.Add(fromEmail)

        Dim toEmail As New sxl.XElement("toEmail")
        toEmail.Value = config.toEmail
        sl.Add(toEmail)

        Dim emailSubject As New sxl.XElement("emailSubject")
        emailSubject.Value = config.emailSubject
        sl.Add(emailSubject)

        Dim emailUserId As New sxl.XElement("emailUserId")
        emailUserId.Value = config.emailUserId
        sl.Add(emailUserId)

        Dim emailPassword As New sxl.XElement("emailPassword")
        emailPassword.Value = config.emailPassword
        sl.Add(emailPassword)

        Dim serverHost As New sxl.XElement("serverHost")
        serverHost.Value = config.serverHost
        sl.Add(serverHost)

        Dim serverPort As New sxl.XElement("serverPort")
        serverPort.Value = config.serverPort
        sl.Add(serverPort)

        Dim ratingThreashold As New sxl.XElement("ratingThreashold")
        ratingThreashold.Value = config.ratingThreashold
        sl.Add(ratingThreashold)

        Dim k As Integer
        For Each k In config.channelList.Keys
            Dim s As New sxl.XElement("service")
            sl.Add(s)
            s.SetAttributeValue("serviceNum", k.ToString)
            s.SetAttributeValue("channelName", config.channelList.Item(k).channelName)
            s.SetAttributeValue("onId", config.channelList.Item(k).onId)
            s.SetAttributeValue("tsId", config.channelList.Item(k).tsId)
        Next

        doc.Save(My.Application.Info.DirectoryPath & "\serviceNames.xml")

        Dim sl2 As New sxl.XElement("watchList")
        Dim doc2 As New sxl.XDocument
        doc2.Add(sl2)

        For Each p As String In progWatchList
            If p = "" Then Continue For
            Dim s As New sxl.XElement("program")
            sl2.Add(s)
            s.SetAttributeValue("name", p)
        Next

        For Each c As String In catWatchList
            If c Is Nothing Then Continue For
            Dim s As New sxl.XElement("Category")
            sl2.Add(s)
            s.SetAttributeValue("name", c)
        Next

        My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\watchNames.xml")
        doc2.Save(My.Application.Info.DirectoryPath & "\watchNames.xml")

    End Sub

End Class
